using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace Impact.Model.Objects
{
    /// <summary>
    /// Extensions for PagePluginCollection
    /// </summary>
    public partial class PagePluginCollection : ISerializable
    {

        /// <summary>
        /// uses the current order of the collection and assigns new sequence numbers
        /// </summary>
        public void Resequence()
        {

            int count = 10;
            foreach (PagePlugin plugin in this)
            {
                plugin.Sequence = count;
                count += 10;
            }

        }

        /// <summary>
        /// decrements the sequence of the plugin, so it moves up (vertically) in the list
        /// </summary>
        /// <param name="registrationId">the registration id of the PagePlugin object to move</param>
        public void MoveUp(int registrationId)
        {
            this.Resequence();
            PagePlugin plugin = (PagePlugin)this.Find( registrationId );
            plugin.Sequence -= 11;
            this.Sort( PagePlugin.Columns.Sequence, true );
            this.Resequence();
        }

        /// <summary>
        /// decrements the sequence of the plugin, so it moves up (vertically) in the list
        /// </summary>
        /// <param name="registrationId">the registration id of the PagePlugin object to move</param>
        public void MoveDown(int registrationId)
        {
            this.Resequence();
            PagePlugin plugin = (PagePlugin)this.Find( registrationId );
            plugin.Sequence += 11;
            this.Sort( PagePlugin.Columns.Sequence, true );
            this.Resequence();
        }


        #region ISerializable Members

        /// <summary>
        /// constructor for serialization
        /// </summary>
        public PagePluginCollection(SerializationInfo info, StreamingContext context)
        {
            foreach (SerializationEntry entry in info)
            {
                if (entry.ObjectType.Equals(typeof(PagePlugin)))
                {
                    PagePlugin plugin = (PagePlugin)entry.Value;
                    this.Add(plugin);
                }
            }
        }

        /// <summary>
        /// accessor for serialization
        /// </summary>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            foreach (PagePlugin plugin in this)
            {
                info.AddValue(Guid.NewGuid().ToString(), plugin);
            }

        }

        #endregion


    }
}
